// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
declare const pomelo;
declare const wx;
import  LoginType from './common/enum/login_type'
import net_result from './common/enum/net_result';
import constants from './constants';
import UIController from './ui_controller';

const host="tc.mofangapp.com";
// const host="test.com";
   
@ccclass
export default class NewClass extends cc.Component {

 
    // LIFE-CYCLE CALLBACKS:
 
    onLoad () {

        pomelo.init({
            host: host,
          }, function() {
            console.log('connect..success');
            if(window['wx']){
                wx.login({
                    success: (res) =>{
                        if(res.code){
                           console.log('wx.login.success',res);
                             pomelo.request('connector.entryHandler.entry', {code: res.code,loginType:LoginType.WechatMini}, (data)=> {
                                 console.log(data);
                                 if(data.code===net_result.code.SUCCESS){
                                    wx.getUserInfo({  
                                        lang: "zh_CN",  
                                        success:  (userRes)=> {  
                                          console.log(userRes);
                                          pomelo.request('connector.entryHandler.loginByWechatMini2', {  
                                            code: res.code,  
                                            encryptedData: userRes.encryptedData,  
                                            iv: userRes.iv,
                                            session_key:data.data.session_key
                                          }, function(data) {
                                          
                                             
                                          })  
                                        },
                                        fail:function(error){
                                            console.log(error);
                                        }  
                                      })  
                                 }
                            });
                        }
                        else{
                            console.log('wx.login.err',res);
                        }
                    },fail:(error)=>{
                        console.log(error);
                    }
                  });
            }
            
          
          });

          if(window["wx"]){
            wx.createGameClubButton({
                icon: 'white',
                style: {
                    left: 10,
                    bottom: 10,
                    width: 30,
                    height: 30
                }
            })
          }
    }

    start () {

    }
    onStartClick(){
        cc.director.emit(constants.GAME_EVENT.BtnClick);
        cc.director.loadScene('game_main'); 
    }
    onRankingClick(){
        UIController.getInst().showWithMask(UIController.UiPrefabPath.Ranking);
    }
    onInviteClick(){
        if(window["wx"]){
            wx.shareAppMessage({title:"一起来玩弹球!!",success:()=>{
                console.log("分享成功");
            },fail:()=>{

            }})

        }
 
    }
    // update (dt) {}
}
